package com.itboye.ihomebank.adapter;

import java.util.Iterator;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.bean.WoDeQiuZuBean;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.presenter.HousePresenter;
import com.itboye.ihomebank.support.commonadapter.MutiplyCommonAdapter;
import com.itboye.ihomebank.support.commonadapter.ViewHolder;
import com.itboye.ihomebank.util.ByAlert;
import com.itboye.ihomebank.util.SPUtils;

/***
 * 示例adapter
 * 
 * @author mr.w
 * 
 */
public class WodeYuYueAdapter extends MutiplyCommonAdapter<WoDeQiuZuBean>
		implements Observer {
	List<WoDeQiuZuBean> beans;
	int position;
	HousePresenter housePresenter;
	protected String uid;
	protected String tempHouseNo;

	public WodeYuYueAdapter(Context context, List<WoDeQiuZuBean> datas,
			int... layoutId) {
		super(context, datas, layoutId);
		// TODO Auto-generated constructor stub
		beans = datas;
		housePresenter = new HousePresenter(this);
		uid = SPUtils.get(context, null, SPContants.USER_ID, "") + "";
	}

	@Override
	public int getItemViewType(int position) {
		// TODO Auto-generated method stub

		return 0;
	}

	@Override
	public void convert(ViewHolder holder, final WoDeQiuZuBean t, int type,
			final int position) {
		// TODO Auto-generated method stub
		holder.setText(R.id.address, t.getAddressDetail() + "");

		holder.setText(R.id.fangyuanzhuangtai, "审核中");
		holder.setTag(R.id.cancel, t.getId());
		String status = t.getApplyStatus();
		if (status.equals("1")) {
			holder.setText(R.id.shenhezhuangtai, "审核中");
		} else if (status.equals("2")) {
			holder.setText(R.id.shenhezhuangtai, "已通过");
		}

		holder.setOnClickListener(R.id.cancel, position, new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				tempHouseNo = t.getHouseNo();
				housePresenter.yuYueKanFang(uid, t.getHouseNo(), "", "", "",
						"2");
			}
		});
	}

	@Override
	public void getItemPosition(int position) {
		// TODO Auto-generated method stub
		this.position = position;
	}

	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = handlerError(data);
		if (entity != null) {
			if (entity.getEventType() == HousePresenter.yuYueKanFang_success) {
				ByAlert.alert(entity.getData());
				Iterator<WoDeQiuZuBean> iterator = beans.iterator();
				while (iterator.hasNext()) {
					WoDeQiuZuBean bean = iterator.next();
					if (tempHouseNo.equals(bean.getHouseNo())) {
						iterator.remove();
						notifyDataSetChanged();
						break;
					}

				}
			} else if (entity.getEventType() == HousePresenter.yuYueKanFang_fail) {
				ByAlert.alert(entity.getData());
			}
		}
	}

}
